package com.seawaterbt.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seawaterbt.ssm.config.DataSource;
import com.seawaterbt.ssm.config.DataSourceEnum;
import com.seawaterbt.ssm.entity.*;
import com.seawaterbt.ssm.mapper.ShengDatCrewDataMapper;
import com.seawaterbt.ssm.mapper.ShengDatHeatSourceOutletDataMapper;
import com.seawaterbt.ssm.service.CzBasHeatsourceExitService;
import com.seawaterbt.ssm.service.CzDatHeatSourceOutletDataService;
import com.seawaterbt.ssm.service.ShengDatCrewDataService;
import com.seawaterbt.ssm.service.ShengDatHeatSourceOutletDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhf
 * @since 2019-10-31
 */
@Service
public class ShengDatHeatSourceOutletDataServiceImpl extends ServiceImpl<ShengDatHeatSourceOutletDataMapper, ShengDatHeatSourceOutletData> implements ShengDatHeatSourceOutletDataService {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    CzDatHeatSourceOutletDataService datHeatSourceOutletDataService;
    @Autowired
    CzBasHeatsourceExitService basHeatsourceExitService;

    @DataSource(DataSourceEnum.DB1)
    @Override
    public boolean saveOrUpdate(ShengDatHeatSourceOutletData entity) {
        return super.saveOrUpdate(entity);
    }

    @DataSource(DataSourceEnum.DB1)
    @Override
    public ShengDatHeatSourceOutletData getByCodeTime(QueryWrapper wrapper) {
        return this.getOne(wrapper);
    }

    @DataSource(DataSourceEnum.DB1)
    @Override
    public void sendData() {
        QueryWrapper<CzDatHeatSourceOutletData> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("heat_source_outlet_code", 130981011).or().likeRight("heat_source_outlet_code", 130903007);
        List<CzDatHeatSourceOutletData> czData = datHeatSourceOutletDataService.list(queryWrapper);
        Date date = new Date();
        for (CzDatHeatSourceOutletData czdatData : czData) {
            CzBasHeatsourceExit czBasData = basHeatsourceExitService.listOne(czdatData.getHeatSourceOutletCode());
            if (czBasData != null && czBasData.getCodeProvince() != null && !czBasData.getCodeProvince().equals("")) {
                QueryWrapper wrapper = new QueryWrapper();
                wrapper.eq("HeatSourceOutletID", czBasData.getCodeProvince());
                wrapper.eq("GetTime", czdatData.getGetTime());
                ShengDatHeatSourceOutletData byCodeTime = getByCodeTime(wrapper);
                if (byCodeTime == null) {
                    ShengDatHeatSourceOutletData datData = new ShengDatHeatSourceOutletData();
                    datData.setHeatSourceOutletId(czBasData.getCodeProvince());
                    datData.setAddTime(date);
                    datData.setGetTime(czdatData.getGetTime());
                    datData.setBackPre(Double.valueOf(czdatData.getBackPre()));
                    datData.setBackTemp(Double.valueOf(czdatData.getBackTemp()));
                    datData.setCumulativeHeat(Double.valueOf(czdatData.getCumulativeHeat()));
                    datData.setInstantaneousFlow(Double.valueOf(czdatData.getInstantaneousFlow()));
                    datData.setSupplyPre(Double.valueOf(czdatData.getSupplyPre()));
                    datData.setSupplyTemp(Double.valueOf(czdatData.getSupplyTemp()));
                    this.save(datData);
                }
            }
        }

        System.out.println("完成同步热源出口到中间库 " + df.format(new Date()));
    }

    @DataSource(DataSourceEnum.DB1)
    @Override
    public boolean save(ShengDatHeatSourceOutletData entity) {
        return super.save(entity);
    }
}
